package com.hnust.root.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnust.root.common.base.exception.BusinessException;
import com.hnust.root.common.base.status.UserStatus;
import com.hnust.root.common.constant.GlobalConstant;
import com.hnust.root.dao.entity.domain.Manager;
import com.hnust.root.dao.entity.domain.ManagerToRole;
import com.hnust.root.dao.entity.domain.Role;
import com.hnust.root.dao.mapper.ManagerMapper;
import com.hnust.root.dao.mapper.ManagerToRoleMapper;
import com.hnust.root.dao.mapper.RoleMapper;
import com.hnust.root.service.auth.ManagerService;
import com.hnust.root.web.shiro.ShiroKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @Author zhusir
 * @Date 2019/1/27
 * @Desc
 */
@Service
public class ManagerServiceImpl implements ManagerService {

    @Value("${jwt.secret}")
    private String jwtSecret;

    @Autowired
    private ManagerMapper managerMapper;

    @Autowired
    private ManagerToRoleMapper managerToRoleMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public Manager addManager(Manager manager) throws BusinessException {
        Manager user = managerMapper.existManage(manager);
        if (user != null) {
            throw new BusinessException("用户已存在");
        } else {
            String salt = manager.getSalt();
            if (StrUtil.isEmpty(salt)) {
                //用户没有指定加密密钥，使用默认加密密钥
                salt = jwtSecret;
                manager.setSalt(salt);
            }
            String encodePass = ShiroKit.md5(manager.getPassword(), manager.getUsername() + manager.getSalt());
            manager.setPassword(encodePass);
            manager.setCreateTime(LocalDateTime.now());
            manager.setUpdateTime(LocalDateTime.now());
            manager.setState(UserStatus.NOMAL.getCode());

            //增加用户
            manager = managerMapper.insert(manager) > 0 ? manager : null;
            if (manager != null) {
                Role role = roleMapper.selectByRole(GlobalConstant.DEFAULT_ROLE);
                ManagerToRole managerToRole = assmbleManageToRole(manager, role);
                //为用户分配角色
                managerToRoleMapper.insert(managerToRole);
            }
        }
        return manager;
    }

    private ManagerToRole assmbleManageToRole(Manager manager, Role role) {
        ManagerToRole managerToRole = new ManagerToRole();
        managerToRole.setCreatedTime(LocalDateTime.now());
        managerToRole.setManagerId(manager.getId());
        managerToRole.setRoleId(role.getId());
        managerToRole.setUpdatedTime(LocalDateTime.now());
        return managerToRole;
    }

    @Cacheable(value="manager-info",unless = "#result == null ")
    public Manager getManageByUserName(String userName) {
        return managerMapper.selectOne(new QueryWrapper<Manager>().eq("username", userName));
    }
}
